const BaseHandler = require('../base-handler');
const SystemServices = require('../../../constants').SystemServices;
const HTTP_VERBS = require('../../../constants').HTTP_VERBS;

class UserAvtarHandler extends BaseHandler {
    get pattern() {
        return "/upload/userAvatar"
    }


    doPOST(request, response) {
        if (!request.session.user.authenticated) {
            response.json({
                success: false,
                error: '非法操作!'
            });
        } else {
            var base64Str = request.body.avatar;
            var params = { base64Str, storeId: request.session.user.storeId, userId: request.session.user.id };
            params.base64Str = params.base64Str.substring(base64Str.indexOf('base64') + 7);
            this.Controller.changeUserAvatar(params, function (result) {
                if (result.status) {
                    request.session.user.headImage = result.data;
                    response.json({
                        success: true,
                        result: result
                    })
                } else {
                    response.json({
                        success: false,
                        error: result.error
                    })
                }
            });
        }
    }

    setup() {
        this.registerVerb(HTTP_VERBS.GET, this.doGET);
        this.registerVerb(HTTP_VERBS.POST, this.doPOST);
    }
}

module.exports = UserAvtarHandler;